package se.rende.myrss.data;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class ChannelUtil {
	public static ChannelInfo getChannelInfo(MyRSSDataHelper dataHelper, String topId) {
		SQLiteDatabase db = dataHelper.getReadableDatabase();
		Cursor cursor = null;
		try {
			if ("0".equals(topId)) {
				cursor = db.rawQuery("select _id, name, url, is_group from channel", new String[] {});
				List<Channel> channels = new ArrayList<Channel>();
				if (cursor != null) {
					while (cursor.moveToNext()) {
						String id = cursor.getString(0);
						String name = cursor.getString(1);
						String url = cursor.getString(2);
						channels.add(new Channel(id, name, url));
					}
				}
				return new ChannelInfo(topId, "All", channels);
			} else {
				cursor = db.rawQuery("select name, url, is_group from channel where _id=?", new String[] {topId});
				if (cursor != null) {
					if (cursor.moveToFirst()) {
						String name = cursor.getString(0);
						String url = cursor.getString(1);
						return new ChannelInfo(topId, name, Arrays.asList(new Channel(topId, name, url)));
					}
				}
				throw new IllegalArgumentException("channel id " + topId + " not found.");
			}
		} catch (Exception e) {
			throw new IllegalArgumentException(e);
		} finally {
			if (cursor != null) {
				cursor.close();
			}
		}
	}
	
	public static class Channel {
		public String id;
		public String name;
		public String url;

		public Channel(String id, String name, String url) {
			this.id = id;
			this.name = name;
			this.url = url;
		}
		
		@Override
		public String toString() {
			return "Channel(" + id + ", " + name + ", " + url + ")";
		}
	}

	public static class ChannelInfo {
		public String name;
		public List<Channel> channels;
		private String id;

		public ChannelInfo(String id, String name, List<Channel> channels) {
			this.id = id;
			this.name = name;
			this.channels = channels;
		}

		public Channel getChannelBySourceTitle(String sources, String source) {
			Log.d("ChannelUtil.ChannelInfo", "getChannelBySourceTitle(" + sources + ", " + source + ")");
			
			try {
				String[] split = sources.split("&");
				for (int i = 0; i < split.length; i++) {
					String listSource = URLDecoder.decode(split[i], "utf-8");
					if (source != null && source.equals(listSource)) {
						return channels.get(i);
					}
				}
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
			return null;
		}
		
		@Override
		public String toString() {
			return "name=" + name + " channels=" + channels;
		}

		public String getChannelIdCond() {
			if ("0".equals(id)) {
				return null;
			}
			StringBuilder cond = new StringBuilder();
			if (channels.size() > 1) {
				cond.append("(");
			}
			for (Channel channel : channels) {
				if (cond.length() > 0) {
					cond.append(" or ");
				}
				cond.append("channel=" + channel.id);
			}
			if (channels.size() > 1) {
				cond.append(")");
			}
			return cond.toString();
		}
	}
	
	public static String addCond(String cond, String newCond) {
		if (cond == null || cond.trim().isEmpty()) {
			return newCond;
		} else if (newCond == null || newCond.trim().isEmpty()) {
			return cond;
		} else {
			return cond + " " + "and" + " " + newCond;
		}
	}

}
